package com.douyuehan.doubao.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.douyuehan.doubao.common.api.ApiResult;
import com.douyuehan.doubao.project.model.dto.CreateTopicDTO;
import com.douyuehan.doubao.project.model.entity.BmsPost;
import com.douyuehan.doubao.project.model.entity.BmsUser;
import com.douyuehan.doubao.project.model.vo.PostVO;
import com.douyuehan.doubao.project.service.BmsPostService;
import com.douyuehan.doubao.project.service.BmsUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

import static com.douyuehan.doubao.jwt.JwtUtil.USER_NAME;


@RestController
@RequestMapping("/post")
public class BmsPostController extends BaseController {

    @Resource
    private BmsPostService bmsPostService;

    @Resource
    private BmsUserService bmsUserService;

    @GetMapping("/list")
    public ApiResult<Page<PostVO>> list(@RequestParam(value = "tab", defaultValue = "latest") String tab,
                                        @RequestParam(value = "pageNo", defaultValue = "1")  Integer pageNo,
                                        @RequestParam(value = "size", defaultValue = "10") Integer pageSize) {
        //mybatis自动分页new Page<>(pageNo, pageSize)
        Page<PostVO> list = bmsPostService.getList(new Page<>(pageNo, pageSize), tab);
        return ApiResult.success(list);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ApiResult<BmsPost> create(@RequestHeader(value = USER_NAME) String userName
            , @RequestBody CreateTopicDTO dto) {
        BmsUser user = bmsUserService.getUserByUsername(userName);
        BmsPost topic = bmsPostService.create(dto, user);//谁发表的啥
        return ApiResult.success(topic);
    }

    @GetMapping()
    public ApiResult<Map<String, Object>> view(@RequestParam("id") String id) {
        Map<String, Object> map = bmsPostService.viewTopic(id);
        return ApiResult.success(map);
    }

}
